//
//  imgDetailViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/3/16.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "imgDetailViewController.h"
#import "WMPhotoBrowser.h"
#import "movie-Bridging-Header.h"
@interface imgDetailViewController ()
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, strong)NSMutableArray *DateArr;
@property (nonatomic, strong)NSMutableArray *imgArr;
@end

@implementation imgDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCustomTitleWithTitle:@"事件详情"];
    [self addCustomBackBarButtonItemWithTarget:self action:@selector(backUp)];
    [self analysisDate];
    [self setupUI];
}
- (void)setupUI{

    [self addScrollView];
    [self creatUI];
}
- (void)analysisDate{
    [self.DateArr removeAllObjects];
    NSArray *text = [self.textss componentsSeparatedByString:@","];
    [self.DateArr addObjectsFromArray:text];
}
- (void)addScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
}
- (void)creatUI{
    [self.imgArr removeAllObjects];
    [self.imgArr addObjectsFromArray:self.DateArr];
    NSLog(@"%@",self.DateArr);
    CGFloat orY = 0;
    if(self.DateArr.count == 0){
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectZero];
        lab.text = @"小编正在火速获取详情,请稍候";
        lab.textColor = [UIColor colorWithHexString:BH_Color_Main];
        lab.width = ScreenWidth - PXChange(200);
        lab.numberOfLines = 0;
        [lab sizeToFit];
        lab.center = CGPointMake(ScreenWidth/2.0f, (ScreenHeight - 64)/2.0f);
        [self.scrollView addSubview:lab];
        return;
    }
    CGFloat heigth = ScreenWidth*16.0f/9.0f;
    for(int i = 0;i<self.DateArr.count;i++){
        NSString *url = self.DateArr[i];
//        if([url hasSuffix:@"gif"]){
//            FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
//            [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"gifhold"]];
//            imageView.runLoopMode = NSDefaultRunLoopMode;
//            imageView.contentMode = UIViewContentModeScaleAspectFill;
//            imageView.frame = CGRectMake(0.0, orY, ScreenWidth, heigth);
//            [self.scrollView addSubview:imageView];
//            orY = imageView.bottom;
//        }else{
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, orY + PXChange(10), ScreenWidth, heigth)];
            [img sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"jpghold"]];
            img.contentMode = UIViewContentModeScaleAspectFit;
            img.userInteractionEnabled = YES;
        img.tag = 1000 + i;
            [self.scrollView addSubview:img];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
            [img addGestureRecognizer:tap];
            orY = img.bottom;
//        }
    }
    [self.scrollView setContentSize:CGSizeMake(ScreenWidth, orY+ PXChange(200))];
}

-(void)tapClick:(UITapGestureRecognizer *)tap{
    UIImageView *im =(UIImageView *)tap.view;
    NSInteger index = im.tag - 1000;
    WMPhotoBrowser *browser = [WMPhotoBrowser new];
    browser.dataSource =self.imgArr.mutableCopy;
    browser.downLoadNeeded = NO;
    browser.currentPhotoIndex = index;
    [self.navigationController pushViewController:browser animated:YES];
    
}
- (NSMutableArray *)DateArr{
    if(!_DateArr){
        _DateArr = [[NSMutableArray alloc]init];
    }
    return _DateArr;
}
- (NSMutableArray *)imgArr{
    if(!_imgArr){
        _imgArr = [[NSMutableArray alloc]init];
    }
    return _imgArr;
}

@end
